/*
 * main.c
 *
 *  Created on: Aug 7, 2013
 *      Author: lxzhu
 */

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
//定义全局的顶点数组
GLfloat vertices[][3] = { { -1.0, -1.0, 1.0 }, { -1.0, 1.0, 1.0 }, { 1.0, 1.0,
		1.0 }, { 1.0, -1.0, 1.0 }, { -1.0, -1.0, -1.0 }, { -1.0, 1.0, -1.0 }, {
		1.0, 1.0, -1.0 }, { 1.0, -1.0, -1.0 } };

//定义全局的颜色数组
GLfloat colors[][3] = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 1.0 }, { 1.0, 1.0, 0.0 },
		{ 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 0.0, 1.0 } };
void init();
void draw();
void polygon(int a, int b, int c, int d);
void cube();


int main(int argc, char** argv){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(800,600);
	glutCreateWindow("Vertices");
	glutDisplayFunc(draw);
	init();
	glutMainLoop();
	return 0;

}
void init(){
	glShadeModel(GL_SMOOTH);
	glMatrixMode(GL_PROJECTION);
	gluLookAt(1,1,1,0,0,0,0,0,1);
	glMatrixMode(GL_MODELVIEW);
}
void draw(){
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glScalef(0.8,0.8,0.8);
	cube();
	glPopMatrix();

	glutSwapBuffers();
}
void polygon(int a, int b, int c, int d) {
	glBegin(GL_POLYGON);
	glVertex3fv(vertices[a]);
	glVertex3fv(vertices[b]);
	glVertex3fv(vertices[c]);
	glVertex3fv(vertices[d]);
	glEnd();
}
void cube() {
	glColor3fv(colors[0]);
	polygon(0, 3, 2, 1);

	glColor3fv(colors[2]);
	polygon(7, 6, 2, 3);

	glColor3fv(colors[3]);
	polygon(4, 5, 6, 7);

	glColor3fv(colors[0]);
	polygon(4, 0, 1, 5);

	glColor3fv(colors[4]);
	polygon(0, 3, 7, 4);

	glColor3fv(colors[5]);
	polygon(1, 2, 6, 5);
}
